<?php
class FamiliasController extends AppController {

	var $name = 'Familias';
	var $components = array('RequestHandler');
	
	function index(){
		$this->layout="ajax";		
		
		if($this->JQGridPaginate->isGrid){
			$fields = array('Familia.id','Familia.codigo','Familia.descripcion','"Familia"."created"::date AS "Familia__created"','"Estado"."descripcion" AS "Familia__estado"');
			$this->JQGridPaginate->recursive = 0;
			$this->JQGridPaginate->cakefields = $fields;
			echo $this->JQGridPaginate->paginate("Familia");
			$this->autoRender = false;
		}elseif($this->RequestHandler->isPost()){	
			$oper = $this->params['form']['oper'];
			if($oper == "add"){
				$this->Familia->create();				 
			}elseif ($oper == "edit"){
				$this->data['Familia']['id'] = $this->params['form']['id'];
			}elseif ($oper == "del"){
				$this->data['Familia']['id'] = $this->params['form']['id'];
				$this->data['Familia']['estado_id'] = 2;				
			}
			
			@$this->Familia->save($this->data['Familia']);
			echo json_encode($this->Familia->visualizarError($this->Familia->validationErrors));
			$this->autoRender = false;
		}		
	}
	
	function listafamilias(){
		$this->layout="ajax";
		$listaFamilias = $this->Familia->find('list',array('fields'=>array('Familia.id','Familia.descripcion'),
															'order'=>'Familia.descripcion'));
		echo json_encode($listaFamilias);
		$this->autoRender = false;
	}
}